package {{.packageName}}

import(
    "time"
    "codeGenerator/variable"
    {{- range $importName := .importNameList}}
    {{$importName}}
    {{- end}}
)

type {{.structName}} struct{
    Id {{.pkType}}
    {{- range .fieldNameList}}
    {{.FieldName}} {{.FieldType}}
    {{- end}}
    CreatedAt time.Time
    UpdatedAt time.Time
}

func New{{.structName}}() *{{.structName}} {
	return &{{.structName}}{
	    {{- range .fieldNameList}}
	    {{- if eq .isNew true}}
	    {{.FieldName}}:{{.FieldPackageName}}.New{{.FieldName}}(),
	    {{- end}}
	    {{- end}}
	}
}

func ({{.objectName}} *{{.structName}}) Created{{.structName}}(o *{{.structName}}) map[string]interface{} {
    variable.DB.Create(o)

    return map[string]interface{}{"code":"200", "data":nil, "message":"成功"}
}

func ({{.objectName}} *{{.structName}}) update{{.structName}}(o *{{.structName}}) map[string]interface{} {
    variable.DB.Create(o)

    return map[string]interface{}{"code":"200", "data":nil, "message":"成功"}
}

func ({{.objectName}} *{{.structName}}) delete{{.structName}}(id int64) map[string]interface{} {
    variable.DB.Delete(&{{.structName}}{}, id)

    return map[string]interface{}{"code":"200", "data":nil, "message":"成功"}
}

func ({{.objectName}} *{{.structName}}) Find{{.structName}}All(condition map[string]interface{}) []*{{.structName}}{
    var list []*{{.structName}}

    pageSize := condition["pageSize"].(int)
    pageNo := condition["pageNo"].(int)

    delete(condition, "pageSize")
    delete(condition, "pageNo")

    variable.DB.Limit(pageSize).
    Offset((pageNo-1)*pageSize).
    Where(condition).
    Find(&list)

    return list
}

func ({{.objectName}} *{{.structName}}) Find{{.structName}}One(id int64) *{{.structName}}{
    var o {{.structName}}
    variable.DB.Where("id = ?", id).First(&o)

    return &o
}
